package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCrmHighseasService;
import com.mdd.admin.validate.ClientCrmHighseasCreateValidate;
import com.mdd.admin.validate.ClientCrmHighseasUpdateValidate;
import com.mdd.admin.validate.ClientCrmHighseasSearchValidate;
import com.mdd.admin.vo.ClientCrmHighseasListedVo;
import com.mdd.admin.vo.ClientCrmHighseasDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmHighseas;
import com.mdd.common.mapper.ClientCrmHighseasMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户公海实现类
 * @author LikeAdmin
 */
@Service
public class ClientCrmHighseasServiceImpl implements IClientCrmHighseasService {
        
    @Resource
    ClientCrmHighseasMapper clientCrmHighseasMapper;

    /**
     * 客户公海列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmHighseasListedVo>
     */
    @Override
    public PageResult<ClientCrmHighseasListedVo> list(PageValidate pageValidate, ClientCrmHighseasSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmHighseas> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        clientCrmHighseasMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:clientGradeid@client_gradeId:int",
            "between:openSeaTime@open_sea_time:str",
            "=:oldBusinessStaffid@old_business_staffId:int",
        });

        IPage<ClientCrmHighseas> iPage = clientCrmHighseasMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmHighseasListedVo> list = new LinkedList<>();
        for(ClientCrmHighseas item : iPage.getRecords()) {
            ClientCrmHighseasListedVo vo = new ClientCrmHighseasListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户公海详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ClientCrmHighseas
     */
    @Override
    public ClientCrmHighseasDetailVo detail(Integer id) {
        ClientCrmHighseas model = clientCrmHighseasMapper.selectOne(
                new QueryWrapper<ClientCrmHighseas>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmHighseasDetailVo vo = new ClientCrmHighseasDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户公海新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmHighseasCreateValidate createValidate) {
        ClientCrmHighseas model = new ClientCrmHighseas();
        model.setClientGradeid(createValidate.getClientGradeid());
        model.setOpenSeaTime(createValidate.getOpenSeaTime());
        model.setOldBusinessStaffid(createValidate.getOldBusinessStaffid());
        model.setClientName(createValidate.getClientName());
        model.setClientWho(createValidate.getClientWho());
        model.setPhone(createValidate.getPhone());
        model.setMail(createValidate.getMail());
        model.setOpenSeaPool(createValidate.getOpenSeaPool());
        clientCrmHighseasMapper.insert(model);
    }

    /**
     * 客户公海编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmHighseasUpdateValidate updateValidate) {
        ClientCrmHighseas model = clientCrmHighseasMapper.selectOne(
                new QueryWrapper<ClientCrmHighseas>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setClientGradeid(updateValidate.getClientGradeid());
        model.setOpenSeaTime(updateValidate.getOpenSeaTime());
        model.setOldBusinessStaffid(updateValidate.getOldBusinessStaffid());
        model.setClientName(updateValidate.getClientName());
        model.setClientWho(updateValidate.getClientWho());
        model.setPhone(updateValidate.getPhone());
        model.setMail(updateValidate.getMail());
        model.setOpenSeaPool(updateValidate.getOpenSeaPool());
        clientCrmHighseasMapper.updateById(model);
    }

    /**
     * 客户公海删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmHighseas model = clientCrmHighseasMapper.selectOne(
                new QueryWrapper<ClientCrmHighseas>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmHighseasMapper.updateById(model);
    }

}
